package yunhe.biz.impl;

import org.apache.ibatis.session.SqlSession;
import yunhe.biz.IProcureBiz;
import yunhe.dao.IProcureDao;
import yunhe.entity.*;
import yunhe.util.SqlSessionFactoryUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProcureBizImpl implements IProcureBiz {
	/**
	 * 合同的创建
	 */
	public boolean insertPurchasePack(PurchasePack purchasePack) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		boolean flag=procureDao.insertPurchasePack(purchasePack);
		session.commit();
		return flag;
	}

	/**
	 * 根据产品查询合同
	 */
	public PurchasePack selectPurchasePackByProduct(Product product) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		PurchasePack purchasePack=procureDao.selectPurchasePackByProduct(product.getProBarCode());
		return purchasePack;
	}

	/**
	 * 进货单的创建
	 */
	public boolean insertPurchase(Purchase purchase) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		boolean flag=procureDao.insertPurchase(purchase);
		session.commit();	
		return flag;
	}
	/**
	 * 根据供货商和日期查询进货单
	 */
	
	public List<Purchase> selectPurchaseBySupplier(Supplier supplier,String startDate,String endDate) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Map<String,String> inMap=new HashMap<String,String>();
		inMap.put("supplyId", supplier.getSupId());
		inMap.put("startDate",startDate);
		inMap.put("endDate",endDate);
		List<Purchase> list=procureDao.selectPurchaseBySupplier(inMap);
		return list;
	}

	/**
	 * 根据进货日期查询进货单
	 */
	public List<Purchase> selectPurchaseByPurchaseDate(String startDate,
			String endDate,int pageSize,int currPage) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Map<String,Object> inMap=new HashMap<String,Object>();
		inMap.put("startDate",startDate);
		inMap.put("endDate",endDate);
		inMap.put("pageSize",pageSize);
		inMap.put("currPage",(currPage-1)*pageSize);
		List<Purchase> list=procureDao.selectPurchaseByPurchaseDate(inMap);
		return list;
	}

	/**
	 * 创建采购单
	 */
	public boolean insertPurchaseAppend(PurchaseAppend purchaseAppend) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		boolean flag=procureDao.insertPurchaseAppend(purchaseAppend);
		return flag;
	}

	/**
	 * 根据产品分页查询采购单
	 */
	public List<PurchaseAppend> selectPurchaseAppendByProduct(int pageSize,int currPage,Product product) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		List<PurchaseAppend> list=procureDao.selectPurchaseAppendByProducts(pageSize,(currPage-1)*pageSize,product.getProBarCode());
		return list;
	}

	/**
	 * 根据采购单查询进货单
	 */
	public Purchase selectPurchaseByPurchaseAppend(PurchaseAppend purchaseAppend) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Purchase purchase=procureDao.selectPurchaseByPurchaseAppend(purchaseAppend.getPurchase().getPursId());
		return purchase;
	}

	/**
	 * 根据主表查询从表
	 */
	public PurchaseAppend selectPurchaseAppendByPurchase(Purchase purchase) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		PurchaseAppend purchaseAppend=procureDao.selectPurchaseAppendByPurchase(purchase.getPursId());
		return purchaseAppend;
	}

	/**
	 * 产品信息的录入
	 */
	public boolean insertProduct(Product product) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		boolean flag=procureDao.insertProduct(product);
		session.commit();
		return flag;
	}
	/**
	 * 根据供货商部分信息获取供货商详细信息
	 */
	public Supplier selectSupplierBySupplier(Supplier supplier) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Supplier supplier1=procureDao.selectSupplierBySupplierName(supplier.getSupName());
		return supplier1;
	}
	/**
	 * 根据用户部分信息获取用户详细信息
	 */
	public EmpMsg selectEmpMsgByEmpMsgName(EmpMsg empMsg) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		EmpMsg empMsg1=procureDao.selectEmpMsgByEmpMsgName(empMsg.getName());
		return empMsg1;
	}

	/**
	 * 根据产品编号获取产品信息
	 */
	public Product getByProBarCode(Product product) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Product product1=procureDao.getByProBarCode(product.getProBarCode());
		return product1;
	}

	/**
	 * 分页查询有效采购单
	 */
	public List<PurchaseAppend> selectPurchaseAppendByValidPurQuantity(
			int pageSize, int currPage) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		List<PurchaseAppend> list=procureDao.selectPurchaseAppendByValidPurQuantity(pageSize, (currPage-1)*pageSize);
		return list;
	}

	/**
	 * 有效采购单条数
	 */
	public int selectValidPurQuantity() {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		int count=procureDao.selectValidPurQuantity();
		return count;
	}

	/**
	 * 分页查询退货单
	 */
	public List<PurchaseAppend> selectPurchaseAppendByInvalidPurQuantity(
			int pageSize, int currPage) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		List<PurchaseAppend> list=procureDao.selectPurchaseAppendByInvalidPurQuantity(pageSize, (currPage-1)*pageSize);
		return list;
	}

	/**
	 * 根据仓库名查询仓库详细信息
	 */
	public Storage selectStorageByStorageName(Storage storage) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Storage storage1=procureDao.selectStorageByStorageName(storage.getStoName());
		return storage1;
	}

	/**
	 * 退货单条数
	 */
	public int selectInvalidPurQuantity() {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		int count=procureDao.selectInvalidPurQuantity();
		return count;
	}
	/**
	 * 一段时间内有关该供货商的进货单的条数
	 * @param supplier
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public int selectPurchaseBySupplierDate(Supplier supplier, String startDate,
			String endDate) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Map<String,String> inMap=new HashMap<String,String>();
		inMap.put("supplyId", supplier.getSupId());
		inMap.put("startDate",startDate);
		inMap.put("endDate",endDate);
		int count=procureDao.selectPurchaseBySupplierDate(inMap);
		return count;
	}

	public List<PurchaseAppend> selectPurchaseAppendByProductDate(
			String startDate, String endDate,
			Product product) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Map<String,Object> inMap=new HashMap<String,Object>();
		inMap.put("startDate",startDate);
		inMap.put("endDate",endDate);
		inMap.put("proBarCode",product.getProBarCode());
		List<PurchaseAppend> list=procureDao.selectPurchaseAppendByProductDate(inMap);
		return list;
	}

	public int selectPurchaseAppendByProductDateCount(String startDate,
			String endDate, Product product) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Map<String,Object> inMap=new HashMap<String,Object>();
		inMap.put("startDate",startDate);
		inMap.put("endDate",endDate);
		inMap.put("proBarCode",product.getProBarCode());
		int count=procureDao.selectPurchaseAppendByProductDateCount(inMap);
		return count;
	}

	public List<PurchaseAppend> selectPurchaseAppendBySupplierPagesDate(
			String startDate, String endDate,
			Supplier supplier) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Map<String,Object> inMap=new HashMap<String,Object>();
		inMap.put("startDate",startDate);
		inMap.put("endDate",endDate);
		inMap.put("supplyId",supplier.getSupId());
		List<PurchaseAppend> list=procureDao.selectPurchaseAppendBySupplierPagesDate(inMap);
		return list;
	}

	public int selectPurchaseAppendBySupplierPagesCount(String startDate,
			String endDate, Supplier supplier) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Map<String,Object> inMap=new HashMap<String,Object>();
		inMap.put("startDate",startDate);
		inMap.put("endDate",endDate);
		inMap.put("supplyId",supplier.getSupId());
		int count=procureDao.selectPurchaseAppendBySupplierPagesCount(inMap);
		return count;
	}

	
	public List<PurchaseAppend> selectPurchaseAppendByPages(int pageSize,
			int currPage) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		List<PurchaseAppend> list=procureDao.selectPurchaseAppendByPages(pageSize, (currPage-1)*pageSize);
		return list;
	}

	
	public int selectPurchaseAppendCount() {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		int count=procureDao.selectPurchaseAppendCount();
		return count;
	}

	
	public boolean deletePurchaseAndPurchaseAppendBypurId(String purId) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		boolean flag=procureDao.deletePurchaseAndPurchaseAppendBypurId(purId);
		session.commit();
		return flag;
	}

	public PurchaseAppend selectPurchaseAppendByPurchaseAppend(String purId) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		PurchaseAppend purchaseAppend=procureDao.selectPurchaseAppendByPurchaseAppend(purId);
		return purchaseAppend;
	}

	
	public List<PurchaseAppend> selectInvalidPurchaseAppendByPagesAndDate(
			String startDate, String endDate) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Map<String,Object> inMap=new HashMap<String,Object>();
		inMap.put("startDate",startDate);
		inMap.put("endDate",endDate);
		List<PurchaseAppend> purchaseAppendList=procureDao.selectInvalidPurchaseAppendByPagesAndDate(inMap);
		return purchaseAppendList;
	}

	
	public int selectInvalidPurchaseAppendByPagesAndDateCount(String startDate,
			String endDate) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Map<String,Object> inMap=new HashMap<String,Object>();
		inMap.put("startDate",startDate);
		inMap.put("endDate",endDate);
		int count=procureDao.selectInvalidPurchaseAppendByPagesAndDateCount(inMap);
		return count;
	}

	
	public List<PurchaseAppend> selectValidPurchaseAppendByPagesAndDate(
			String startDate, String endDate) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Map<String,Object> inMap=new HashMap<String,Object>();
		inMap.put("startDate",startDate);
		inMap.put("endDate",endDate);
		List<PurchaseAppend> purchaseAppendList=procureDao.selectValidPurchaseAppendByPagesAndDate(inMap);
		return purchaseAppendList;
	}

	
	public int selectValidPurchaseAppendByPagesAndDateCount(String startDate,
			String endDate) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		Map<String,Object> inMap=new HashMap<String,Object>();
		inMap.put("startDate",startDate);
		inMap.put("endDate",endDate);
		int count=procureDao.selectValidPurchaseAppendByPagesAndDateCount(inMap);
		return count;
	}
	/**
	 * 根据采购单查询进货单
	 */
	public List<PurchaseAppend> selectPurchaseAppend(String prusId)
	{
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		IProcureDao procureDao=(IProcureDao)session.getMapper(IProcureDao.class);
		return procureDao.selectPurchaseAppend(prusId);
	}

}
